package top.humbleyuan.linkedlist;

import top.humbleyuan.datastructure.linkedlist.LinkNode;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 22:05
 * @Des 旋转链表
 */
public class LeetCode_61 {
    public static void main(String[] args) {
        /**
         * 数据准备
         */
        LinkNode head = new LinkNode(1);
        int[] vals = {2,3,4,6,7,8};
        head = LinkNode.formLink(head,vals);
        int k = 7;

        LinkNode pre = new LinkNode(0);
        pre.next = head;

        LinkNode temp = head;
        int i;
        for(i = 1;temp.next != null;i++) {
            temp = temp.next;
        }
        LinkNode end = temp;

        temp = head;
        int n = k % i;
        if(n == 0) {
            LinkNode.headToString(head);
            return ;
        }
        //找到倒数第n+1个位置,正数i-n
        for(int j = 0;j < i - n - 1;j++) {
            temp = temp.next;
        }

        pre.next = temp.next;
        temp.next = null;

        end.next = head;
        LinkNode.headToString(pre.next);
    }
}
